﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

using YT.DB;
using YT.Modules;
using YT.Commons;  

namespace YT.XH.Others
{
    public partial class frmOthersExamBeforeTreatTitle : Form
    {
        private string mstrSickRecID = string.Empty;
        private FormStatus mfrmStatus;
        private string mstrTableName = "ExamBeforTreatResult";
        private string mstrSeqNo = string.Empty;
        ArrayList marrlist = new ArrayList();
        ArrayList marrcon = new ArrayList();
        ArrayList marrTopLeft = new ArrayList();
        public frmOthersExamBeforeTreatTitle()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
            mstrSeqNo = "1";
        }

         public frmOthersExamBeforeTreatTitle(string strSickRecID, FormStatus frmStauts)
        {
            mfrmStatus = frmStauts;
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }

         public frmOthersExamBeforeTreatTitle(string strSickRecID, FormStatus frmStauts, string strExamSeqNo)
        {
            mfrmStatus = frmStauts;
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = strExamSeqNo;
        }

         private void frmOthersExamBeforeTreatTitle_Load(object sender, EventArgs e)
         {
             this.Text = "治疗前检查";
             FillData();
             if (mfrmStatus == FormStatus.Update) pnlDate.Enabled = false;
             this.Refresh();
             timer1.Start();  
         }

         private void btnExit_Click(object sender, EventArgs e)
         {
             if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                 if (!UpdateData(mstrSeqNo))
                 {
                     return;
                 }
             }
             this.Refresh();
             this.Close();
         }

         private void btnNew_Click(object sender, EventArgs e)
         {

         }

         private void btnSave_Click(object sender, EventArgs e)
         {
             DBAccess dbcon = new DBAccess(false, false);
             string strSQL = string.Empty;
             if (!VaildCheck()) return;
             strSQL = " if not exists ( select * from  ExamBeforeTreatHdr where Seqno='" + mstrSeqNo + "' and Type='" + TitleType.OTHERS + "' and SickRecID='" + mstrSickRecID + "')";
             strSQL += " Insert into ExamBeforeTreatHdr(  SickRecID, SeqNo, ExamDate,Type ) " +
                       " values (  '" + mstrSickRecID + "','" + mstrSeqNo + "','" + BaseFunctions.FormatDate(dtpCheckDate.Text, DateFormatOption.DateFmtYMD) + "' " +
                       " ,'" + TitleType.OTHERS + "' ) ";
             dbcon.ExecuteSql(strSQL);

             UpdateData(mstrSeqNo);

             dbcon.CommitTran();
             mfrmStatus = FormStatus.Update;
             this.Close();
         }

         private bool VaildCheck()
         {
             foreach (Control con in marrlist)
             {
                 if (con.Name.IndexOf("pnl_") >= 0)
                 {
                     foreach (Control conDtl in con.Controls)
                     {
                         if (conDtl.Text == "")
                         {
                             MessageBox.Show("必须填写 口服甘油测试前。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                             conDtl.Focus();
                             return false;
                         }
                     }
                 }
             }
             return true;
         }

         private void FindPanel(Control cons)
         {
             foreach (Control con in cons.Controls)
             {
                 if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                 {
                     marrlist.Add(con);
                     marrcon.Add(con.Tag.ToString());
                     marrTopLeft.Add(con.Top + " " + con.Left);
                 }
                 if (con.Controls.Count > 0)
                 {
                     FindPanel(con);
                 }
                 if (con.GetType().Name == "TextBox") con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText);
             }
         }

         private void FillData()
         {
             string strTableName = "ExamBeforTreatResult";
             DBAccess dbcon = new DBAccess(false, false);
             foreach (Control cons in this.Controls)
             {
                 FindPanel(cons);
             }
             //IComparer myComparer = new myReverserClass();
             //marrlist.Sort(0, marrlist.Count-1 , myComparer); 
             foreach (Control conDtl in marrlist)
             {
                 if (conDtl.GetType().Name == "Panel")
                 {
                     string[] strInfoSeqNo = new string[2];
                     strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                     string strHdrSeqNo = strInfoSeqNo[0];
                     string strDtlSeqNo = strInfoSeqNo[1];
                     string strSickRecID = mstrSickRecID;
                     string strColTitle = string.Empty;
                     string strResult = string.Empty;                     
                     foreach (Control conText in conDtl.Controls)
                     {
                         if (conText.Tag != "" && conText.Tag != null)
                         {
                             strColTitle = conText.Tag.ToString();
                             string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  ExamSeqNo='" + mstrSeqNo + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='" + mstrSickRecID + "' and Type='" + TitleType.OTHERS  + "'";
                             DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                             if (drCheck == null) continue;
                             strResult = CvtFunc.ToString(drCheck["Result"]);
                             if (conText.GetType().Name == "TextBox")
                             {
                                 conText.Text = strResult;
                                 if (conText.Text != "")
                                     helpProd.SetHelpString(conText, conText.Text);
                             }
                             else if (conText.GetType().Name == "DateTimePicker")
                             {
                                 if (strResult != "") ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult);
                             }
                             else if (conText.GetType().Name == "RadioButton")
                             {
                                 if (strResult == "false")
                                     ((RadioButton)conText).Checked = false;
                                 else if (strResult == "true")
                                     ((RadioButton)conText).Checked = true;
                             }
                             else if (conText.GetType().Name == "CheckBox")
                             {
                                 if (strResult == "false")
                                     ((CheckBox)conText).Checked = false;
                                 else if (strResult == "true")
                                     ((CheckBox)conText).Checked = true;
                             }
                             else if (conText.GetType().Name == "Label")
                             {
                                 continue;
                             }
                         }
                     }

                 }
             }
             dbcon.CloseCon();
         }

         private bool UpdateData(string strExamSeqNo)
         {
             string strTableName = "ExamBeforTreatResult";
             DBAccess dbcon = new DBAccess(false, false);
             foreach (Control conDtl in marrlist)
             {
                 if (conDtl.GetType().Name == "Panel")
                 {
                     string[] strInfoSeqNo = new string[2];
                     strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                     string strHdrSeqNo = strInfoSeqNo[0];
                     string strDtlSeqNo = strInfoSeqNo[1];
                     string strSickRecID = mstrSickRecID;
                     string strColTitle = string.Empty;
                     string strResult = string.Empty;
                     DataRow drRow = null;
                     string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo";
                     foreach (Control conText in conDtl.Controls)
                     {
                         if (conText.Tag != "" && conText.Tag != null)
                         {
                             if (conText.GetType().Name == "TextBox")
                             {
                                 strResult = conText.Text;
                             }
                             else if (conText.GetType().Name == "DateTimePicker")
                             {
                                 strResult = BaseFunctions.FormatDate(conText.Text, DateFormatOption.DateFmtYMD);
                             }
                             else if (conText.GetType().Name == "RadioButton")
                             {
                                 strResult = (((RadioButton)conText).Checked ? "true" : "false");
                             }
                             else if (conText.GetType().Name == "CheckBox")
                             {
                                 strResult = (((CheckBox)conText).Checked ? "true" : "false");
                             }
                             else if (conText.GetType().Name == "Label")
                             {
                                 strResult = "";
                                 continue;
                             }
                             strColTitle = conText.Tag.ToString();

                             string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and ExamSeqNo='" + mstrSeqNo + "' and SickRecID='" + mstrSickRecID + "' and Type='" + TitleType.OTHERS + "'";
                             bool blnIsNew = false;
                             DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                             if (drCheck == null)
                             {
                                 blnIsNew = true;
                                 drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                             }
                             else
                             {
                                 blnIsNew = false;
                                 drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                             }

                             drRow["SickRecID"] = strSickRecID;
                             drRow["HdrSeqno"] = strHdrSeqNo;
                             drRow["DtlSeqNo"] = strDtlSeqNo;
                             drRow["RowNo"] = strColTitle;
                             drRow["Result"] = strResult;
                             drRow["Type"] = TitleType.OTHERS ;
                             drRow["ExamSeqNo"] = strExamSeqNo;
                             if (blnIsNew)
                                 DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                             else
                                 DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                         }
                     }
                 }
             }
             dbcon.CloseCon();
             return true;
         }

         private void ShowGridPic(object sender, EventArgs e)
         {
             ArrayList marrBone = new ArrayList();
             ArrayList marrAir = new ArrayList();
             Control conCur = sender as Control;
             string strCurPanalName = string.Empty;
             if (conCur.Name == this.btn_Left1.Name) strCurPanalName = "pnl_Left1";
             if (conCur.Name == this.btn_Right1.Name) strCurPanalName = "pnl_Right1";


             foreach (Control con in marrlist)
             {
                 if (con.Name.Length >= 10 && (con.Name.Substring(0, 8) == "pnl_Left" || con.Name.Substring(0, 9) == "pnl_Right"))
                 {
                     if (con.Name.Substring(0, 10) == strCurPanalName)
                     {
                         string strTmpNo = string.Empty;
                         strTmpNo = GetKHz(con.Name);
                         foreach (Control conDtl in con.Controls)
                         {
                             if (conDtl.Tag.ToString().Trim().ToUpper() == "A")
                             {
                                 marrBone.Add(strTmpNo + "_" + conDtl.Text);
                             }
                             else if (conDtl.Tag.ToString().Trim().ToUpper() == "B")
                             {
                                 marrAir.Add(strTmpNo + "_" + conDtl.Text);
                             }
                         }
                     }
                 }
             }
             frmAirBoneGridResult frmResult = new frmAirBoneGridResult();
             frmResult.DrawImaginPic(marrAir, marrBone, false);
             frmResult.ShowDialog();
         }

         private string GetKHz(string strConName)
         {
             string strNo = strConName.Split('_')[2].ToString();
             if (strNo == "1") return "0.25";
             if (strNo == "2") return "0.5";
             if (strNo == "3") return "1";
             if (strNo == "4") return "2";
             if (strNo == "5") return "3";
             if (strNo == "6") return "4";
             if (strNo == "7") return "8";
             return "0";
         }

         private void SelectNextText(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Return)
             //while (((Control)sender).GetType().Name == "TextBox")
             {
                 SelectNextControl((Control)sender, true, true, true, true);
             }
         }

         private void frmMDExamBeforeTreatTitle_FormClosed(object sender, FormClosedEventArgs e)
         {
             SysVar.marrFormList.Remove(this.Name);
             frmGifImage frmGIF = new frmGifImage();
             frmGIF.TopMost = true;
             frmGIF.Show();
         }

         private void timer1_Tick(object sender, EventArgs e)
         {
             if (this.Opacity >= 1)
                 timer1.Stop();
             this.Opacity += 0.04;
         }

    }

}
